<template>
  <div>
    <el-container>
      <el-header>
        <img class="mlogo"
             src="https://img1.baidu.com/it/u=3430690511,3867923153&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=453" alt="">
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>

  </div>
</template>


<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username: 'Hanson',
        password: '111111'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请选输入密码', trigger: 'change'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            const res = await this.$axios.post('/login', this.ruleForm);
            if (res && res.headers && res.headers['authorization']) {
              const jwt = res.headers['authorization'];
              const userInfo = res.data.data;

              console.log(userInfo);

              // 把数据共享出来
              this.$store.commit("SET_TOKEN", jwt);
              this.$store.commit("SET_USERINFO", userInfo);

              // 跳转页面
              this.$router.push("/blogs");
            } else {
              console.error('Invalid response structure:', res);
            }
          } catch (error) {
            if (error.response) {
              // 请求成功发出且服务器也响应了状态码，但状态代码超出了 2xx 的范围
              console.error('Error response:', error.response);
            } else if (error.request) {
              // 请求已经成功发起，但没有收到响应
              console.error('Error request:', error.request);
            } else {
              // 发送请求时出了点问题
              console.error('Error message:', error.message);
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

  // methods: {
  //   submitForm(formName) {
  //     this.$refs[formName].validate((valid) => {
  //       if (valid) {
  //         const _this = this
  //         this.$axios.post('/login', this.ruleForm).then(res => {
  //
  //           const jwt = res.headers['authorization'];
  //
  //           const userInfo = res.data.data
  //
  //           console.log(userInfo)
  //
  //           // 把数据共享出来
  //           _this.$store.commit("SET_TOKEN",jwt)
  //           _this.$store.commit("SET_USERINFO",userInfo)
  //
  //           // 获取
  //           // console.log(this.$store.getters.getUser)
  //
  //           // 跳转页面
  //           _this.$router.push("/blogs")
  //         })
  //       } else {
  //         console.log('error submit!!');
  //         return false;
  //       }
  //     });
  //   },
  //   resetForm(formName) {
  //     this.$refs[formName].resetFields();
  //   }
  // }
}

</script>


<style scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
//background-color: #E9EEF3; color: #333; text-align: center; line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.mlogo {
  height: 60%;
  margin-top: 10px;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}

</style>
